//
//  ZGLGoodsTableViewSelectCell.m
//  Faker
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLGoodsTableViewSelectCell.h"

#import <UIImageView+AFNetworking.h>
#import "ZGLOptionsModel.h"

@interface ZGLGoodsTableViewSelectCell ()

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLabel;


@end


@implementation ZGLGoodsTableViewSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImageView = [[UIImageView alloc] init];
        self.titleLabel = [[UILabel alloc] init];
        self.introLabel = [[UILabel alloc] init];
        self.boderLabel = [[UILabel alloc] init];
        
        [self.contentView addSubview:self.boderLabel];
        [self.contentView addSubview:self.leftImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.introLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = foreColor;
    [self.leftImageView setFrame:CGRectMake(0, 0, ScaleX * 120, ScaleX * 120)];
    [self.titleLabel setFrame:CGRectMake(ScaleX * 140, ScaleY * 40, ScaleX * 200, ScaleY * 20)];
    self.titleLabel.textColor = artiColor;
    [self.introLabel setFrame:CGRectMake(ScaleX * 120, ScaleY * 60, ScaleX * 200, ScaleY * 15)];
    [self.boderLabel setFrame:CGRectMake(ScaleX * 15, ScaleY * 7.5, ScaleX * 90, ScaleX * 90)];
    self.boderLabel.layer.cornerRadius = ScaleX * 45;
    self.boderLabel.layer.masksToBounds = YES;
    self.boderLabel.layer.borderWidth = 1;
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.introLabel.font = [UIFont systemFontOfSize:12];
    self.introLabel.textColor = [UIColor lightGrayColor];
}


- (void)setOptionsModel:(ZGLOptionsModel *)optionsModel {
    if (_optionsModel != optionsModel) {
        _optionsModel = optionsModel;
    }
    [self handleDataWith:optionsModel];
}

- (void)handleDataWith:(ZGLOptionsModel *)optionsModel {
        __weak typeof(self) weakSelf = self;
        self.leftImageView.tintColor = [self getColor:optionsModel.block_color];
        self.boderLabel.layer.borderColor = [self getColor:optionsModel.block_color].CGColor;
        self.titleLabel.text = optionsModel.title;
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:optionsModel.large_pic]];
        [self.leftImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
            [weakSelf.leftImageView setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
            
        }];
}
- (UIColor *)getColor:(NSString *)hexColor
{
    NSString *string = [hexColor substringFromIndex:1];//去掉#号
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    red = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 2;
    green = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 4;
    blue = [self stringToInt:[string substringWithRange:range]];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

- (int)stringToInt:(NSString *)string{
    
    unichar hex_char1 = [string characterAtIndex:0]; ////两位16进制数中的第一位(高位*16)
    int int_ch1;
    if(hex_char1 >= '0' && hex_char1 <='9')
        int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
    else
        int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
    
    unichar hex_char2 = [string characterAtIndex:1]; ///两位16进制数中的第二位(低位)
    int int_ch2;
    if(hex_char2 >= '0' && hex_char2 <='9')
        int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch2 = hex_char2-55; //// A 的Ascll - 65
    else
        int_ch2 = hex_char2-87; //// a 的Ascll - 97
    return int_ch1+int_ch2;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
